package org.jin.jinaicode.core.saver;

import org.jin.jinaicode.ai.model.MultiFileCodeResult;
import org.jin.jinaicode.exception.BusinessException;
import org.jin.jinaicode.exception.ErrorCode;
import org.jin.jinaicode.model.enums.CodeGenTypeEnum;

public class MultiFileCodeFileSaverTemplate extends CodeFileSaverTemplate<MultiFileCodeResult>{
    @Override
    protected CodeGenTypeEnum getCodeType() {
        return CodeGenTypeEnum.MULTI_FILE;
    }

    @Override
    protected void saveFiles(MultiFileCodeResult result, String baseDirPath) {
        writeToFile(baseDirPath, "index.html", result.getHtmlCode());
        writeToFile(baseDirPath, "index.css", result.getCssCode());
        writeToFile(baseDirPath, "index.js", result.getJsCode());

    }
    @Override
    protected void validateInput(MultiFileCodeResult result) {
        if (result == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "result is null");
        }
        // 只要有任意一个代码文件不为空就认为是有效的
        boolean hasAnyCode = (result.getHtmlCode() != null && !result.getHtmlCode().trim().isEmpty()) ||
                            (result.getCssCode() != null && !result.getCssCode().trim().isEmpty()) ||
                            (result.getJsCode() != null && !result.getJsCode().trim().isEmpty());

        if (!hasAnyCode) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "至少需要包含一个有效的代码文件");
        }
    }
}
